25
Easy2Siksha
5. Symbol Resolution
A symbol is any name in your program that represents a function or a variable, such as sum(),
total, or counter. In the object files, these symbols are often just placeholders, as the exact
location is not known yet. The linker’s job is to resolve these symbols—i.e., figure out where each
symbol (function or variable) is located and connect them to the right place.
For instance, if main() calls a function calculate(), the linker makes sure that when main() is
executed, it goes to the right location in memory to execute calculate(). This is symbol resolution
in action.
6. Relocation
When the linker combines object files, it may need to move pieces of code around. This is called
relocation. During this process, the linker adjusts the addresses of variables, functions, and other
parts of the program so that they make sense in the final memory layout of the executable.
Let’s say an object file has a function at memory address 0x3000. After linking, the function
might end up at 0x5000. The linker updates the references in the program so that everything still
works as expected, even though the locations have changed.
Relocation is especially important when the program is large and needs to be loaded into
different locations in memory. The linker ensures that all parts are relocated correctly.
7. Optimizing Code
Linkers can also help optimize code by removing unused parts. If a program imports a library but
never uses some of the functions in that library, the linker can remove these unused functions,
making the program smaller and more efficient.
For example, if your program includes a library for handling graphics, but you never call the
functions for drawing shapes, the linker will omit that part of the library from the final
executable, saving memory and making the program faster.
8. Error Checking
The linker also checks for errors during the linking process. For example, if the linker cannot find
a reference to a function or variable that your program is trying to use, it will generate an error
and tell you what went wrong. This is helpful because it allows developers to catch mistakes
early in the process, such as missing files or misnamed functions.
In Summary
The linker is like a master puzzle solver for your program. It takes different pieces of code (object
files), resolves how they should be connected (external references, libraries, and symbols),
assigns memory addresses, optimizes code by removing unnecessary parts, and ensures
everything works together as a complete program. Without the linker, a program would remain a
collection of separate pieces that cannot function as a whole.
Here’s an analogy to make it clearer: Imagine you’re building a toy model, like a car. Each part—
wheels, body, engine—comes in separate pieces (like object files). The linker is like the